package com.lishang.location.helper.plugin

import android.app.Activity
import android.content.Context

import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.lishang.location.helper.config.Config
import com.lishang.location.helper.manager.SpManager
import com.lishang.location.helper.receiver.MainReceiver
import com.lishang.location.helper.ui.SettingActivity
import com.lishang.location.helper.utils.ResourceUtil
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage
import kotlin.random.Random

class MainHook : IXposedHookLoadPackage {
    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
        if (Config.HOOK_PACKAGE_NAME == lpparam.processName) {
            Log.e("MainHook", "handleLoadPackage ${lpparam.packageName}")

            //注册广播
            XposedHelpers.findAndHookMethod(
                "com.tencent.wework.launch.WwApplicationLike",
                lpparam.classLoader,
                "onCreate",
                object : XC_MethodHook() {
                    override fun afterHookedMethod(param: MethodHookParam) {
                        super.afterHookedMethod(param)
                        val context = param.thisObject as Context
                        Log.e("MainHook", "afterHookedMethod: registerReceiver")
                        //开启广播
                        context.registerReceiver(
                            MainReceiver(),
                            IntentFilter(Config.BROADCAST_RECEIVER_ACTION)
                        )

                    }
                })


            XposedHelpers.findAndHookMethod(
                lpparam.classLoader.loadClass("com.tencent.wework.setting.controller.SettingDetailActivity"),
                "initView",
                SettingDetailActivityHook()
            )


            //打卡页面 标题
//            XposedHelpers.findAndHookMethod("com.tencent.wework.enterprise.attendance.controller.AttendanceActivity2",
//                lpparam.classLoader, "onCreate", Bundle::class.java, object : XC_MethodHook() {
//                    override fun afterHookedMethod(param: MethodHookParam) {
//                        Log.e(
//                            "MainHook",
//                            "com.tencent.wework.enterprise.attendance.controller.AttendanceActivity2 onCreate afterHookedMethod"
//                        )
//                        val activity = param.thisObject as Activity
//                        Toast.makeText(activity, "长按标题进入助手", Toast.LENGTH_SHORT).show()
//                        activity.findViewById<View>(ResourceUtil.getId(activity, "h95")).apply {
//                            findViewById<View>(ResourceUtil.getId(activity, "h87"))
//                                .setOnLongClickListener {
//                                    context.startActivity(Intent(Intent.ACTION_VIEW).apply {
//                                        setClassName(
//                                            "com.lishang.location.helper",
//                                            SettingActivity::class.java.name
//                                        )
//                                    })
//                                    false
//                                }
//                        }
//
//
//                    }
//                })


            val cls = lpparam.classLoader.loadClass("com.tencent.wework.msg.api.LocationDataItem")
            XposedHelpers.findAndHookMethod(cls, "getLatitude", object : XC_MethodHook() {
                override fun beforeHookedMethod(param: MethodHookParam) {
                    val lat = SpManager.getDouble(Config.KEY_LAT)
                    val enable = SpManager.getBoolean(Config.KEY_ENABLED)
                    Log.e("helper MainHook", "lat:${lat} enable:$enable")
                    XposedBridge.log("helper MainHook lat:${lat}")
                    if (enable) {
                        param.result = lat + offset()
                        Log.e("helper MainHook", " result lat:${param.result} ")
                    }
                }
            }
            )

            XposedHelpers.findAndHookMethod(cls, "getLongitude", object : XC_MethodHook() {
                override fun beforeHookedMethod(param: MethodHookParam) {
                    val lng = SpManager.getDouble(Config.KEY_LNG)
                    val enable = SpManager.getBoolean(Config.KEY_ENABLED)
                    Log.e("helper MainHook", "lng:${lng} enable:$enable")
                    XposedBridge.log("helper MainHook lng:${lng}")
                    if (enable) {
                        param.result = lng + offset()
                        Log.e("helper MainHook", " result lng:${param.result} ")

                    }
                }
            }
            )

            lpparam.classLoader.loadClass("com.tencent.wework.common.utils.NetworkUtil").apply {

                XposedHelpers.findAndHookMethod(this, "bAT", object : XC_MethodHook() {
                    override fun beforeHookedMethod(param: MethodHookParam) {
                        val enable = SpManager.getBoolean(Config.KEY_ENABLED)
                        if (enable) {
                            param.result = ""
                        }
                    }
                }
                )

                XposedHelpers.findAndHookMethod(this, "getBSSID", object : XC_MethodHook() {
                    override fun beforeHookedMethod(param: MethodHookParam) {
                        val enable = SpManager.getBoolean(Config.KEY_ENABLED)
                        if (enable) {
                            param.result = ""
                        }
                    }
                }
                )

                XposedHelpers.findAndHookMethod(this, "bAR", object : XC_MethodHook() {
                    override fun beforeHookedMethod(param: MethodHookParam) {
                        val enable = SpManager.getBoolean(Config.KEY_ENABLED)
                        if (enable) {
                            param.result = ""
                        }
                    }
                }
                )

            }

        }
    }

    private fun offset(): Double {
        val offset = SpManager.getInt(Config.KEY_OFFSET).let {
            if (it == 0) 15 else it
        }
        return if ((Math.random() * 2).toInt() % 2 == 0) {
            -(Math.random() * offset)
        } else {
            (Math.random() * offset)
        } / 100000.0

    }
}